﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PCR.Logic
{
    using PCR.Base;

    /// <summary>
    /// Standard curve manager, contains all the operation of the Standard curve
    /// </summary>
    internal class StdCurveManager : IStdCurveManager
    {
        IDatabase _database = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="database">Database interface<param>
        public StdCurveManager(IDatabase database)
        {
            System.Diagnostics.Debug.Assert(database != null);
            this._database = database;
        }

        /// <summary>
        /// Add a new Standard curve, should be a new instance
        /// </summary>
        /// <param name="item">Standard curve instance to add<param>
        public void Add(IStdCurve item)
        {
            Debug.Assert(item != null);
            this._database.StdCurves.Append(item);
        }

        /// <summary>
        /// Add new Standard curve list, should be new instances
        /// </summary>
        /// <param name="items">Standard curve list to add<param>
        public void Add(IList<IStdCurve> items)
        {
            Debug.Assert(items != null);
            this._database.StdCurves.Append(items);
        }

        /// <summary>
        /// Remove the specified standard curve instance
        /// </summary>
        /// <param name="item">standard curve to remove<param>
        public void Remove(IStdCurve item)
        {
            Debug.Assert(item != null);

            // Should remove the active, if the item is the active one
            RemoveActiveIfPossible(item.ObjectID);

            this._database.StdCurves.Delete(item);
        }

        /// <summary>
        /// Remove standard curve by object ID
        /// </summary>
        /// <param name="id">Standard curve object ID<param>
        public void RemoveByID(Guid id)
        {
            // Should remove the active, if the item is the active one
            RemoveActiveIfPossible(id);
            
            this._database.StdCurves.DeleteByID(id);
        }

        /// <summary>
        /// Remove all the Standard curves
        /// </summary>
        public void RemoveAll()
        {
            this._database.StdCurves.DeleteAll();
        }

        /// <summary>
        /// Modify the standard curve
        /// </summary>
        /// <param name="item">Standard curve instance to modify<param>
        public void Modify(IStdCurve item)
        {
            Debug.Assert(item != null);
            this._database.StdCurves.Update(item);
        }

        /// <summary>
        /// Modify the patient report by object ID
        /// </summary>
        /// <param name="id">Patient report object ID<param>
        /// <param name="item">Patient report instance to modify<param>
        public void Modify(Guid id, IStdCurve item)
        {
            Debug.Assert(item != null);
            this._database.StdCurves.UpdateByID(id, item);
        }

        /// <summary>
        /// Get the standard curve by object ID
        /// </summary>
        /// <param name="id">Standard curve object ID to find<param>
        public IStdCurve GetByID(Guid id)
        {
            return this._database.StdCurves.SelectByID(id);
        }

        /// <summary>
        /// Get all the standard curve list
        /// </summary>
        public IList<IStdCurve> List 
        {
            get
            {
                return this._database.StdCurves.SelectAll();
            }
        }

        /// <summary>
        /// Search the standard curve by search filter
        /// </summary>
        /// <param name="filter">Search filter<param>
        public IList<IStdCurve> Search(ISearchFilter<IStdCurve> filter)
        {
            return this._database.StdCurves.Search(filter);
        }

        /// <summary>
        /// Get or set the active standard curve instance,
        /// the virus exponent use this standard curve as the calculation base
        /// return null if not set
        /// The caller should not modify any data stored in this object
        /// </summary>
        public IStdCurve Active 
        {
            get 
            {
                // Not Set, try to load one from database;
                if (this._activeStdCurve == null) 
                {
                    this._activeStdCurve = this._database.StdCurves.Active;
                }
                
                return this._activeStdCurve; 
            }

            set 
            {
                // Just save it 
                this._database.StdCurves.Active = value;
                
                // Set to null for the get function can reload the object from the database again
                this._activeStdCurve = null;
            }
        }

        protected void RemoveActiveIfPossible(Guid id)
        {
            
            if (this.Active != null)
            {
                // Just the right one, remove it
                if (this.Active.ObjectID == id)
                {
                    this.Active = null;
                }
            }
        }

        private IStdCurve _activeStdCurve = null;
    }
}
